<template>
  <div class="rule-manage-detail">
    <div class="btn-wrap">
      <i class="el-icon-arrow-left back-btn" @click="goBack"></i><span style="font-size: 14px;">{{$t('configLazyLoad.intelligentQualityCheck.ruleManage')}}/{{ruleTitle}}</span>
      <el-button type="primary" class="primary_btn fr" @click="saveAndUse" :disabled="sendding">{{$t('configLazyLoad.intelligentQualityCheck.enableNow')}}</el-button>
      <el-button type="primary" class="primary_btn fr" @click="justSave" :disabled="sendding">{{$t('business.staging')}}</el-button>
      <el-button class="primary_btn fr" @click="goBack">{{$t('public.getback')}}</el-button>
    </div>
    <div style="clear:both;"></div>
    <el-row :gutter="60">
      <el-col :span="4" style="font-size:14px;font-weight:bolder;">{{$t('webchat.basicInfo')}}</el-col>
      <el-col :span="20">
        <el-row class="item-line">
          <el-col :span="3">{{$t('report.quaRuleName1')}} <span style="color: red;">*</span> ：</el-col>
          <el-col :span="21"><el-input class="mark_input" :disabled="state==='edit'" v-model="ruleForm.name"></el-input></el-col>
        </el-row>
        <el-row class="item-line">
          <el-col :span="3">{{$t('report.quaRuleType1')}}：</el-col>
          <el-col :span="21">
            <el-radio-group v-model="ruleForm.type">
              <el-radio label="call" :disabled="state==='edit'">{{$t('report.quaCallQuality')}}</el-radio>
              <el-radio label="webchat" :disabled="state==='edit'">{{$t('report.quaWebchatQuality')}}</el-radio>
            </el-radio-group>
          </el-col>
        </el-row>
        <el-row class="item-line">
          <el-col :span="3">{{$t('sms.introduce')}}：</el-col>
          <el-col :span="21">
            <el-input
              style="width: 400px;"
              type="textarea"
              :rows="2"
              :placeholder="$t('public.pleaseInput')"
              v-model="ruleForm.note">
            </el-input>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <div style="border-bottom: 1px solid #ccc;"></div>
    <el-row :gutter="60" >
      <el-col :span="4" style="font-size:14px;font-weight:bolder;">{{$t('configLazyLoad.intelligentQualityCheck.scoreConfig')}}</el-col>
      <el-col :span="20">
        <el-row class="item-line">
          <el-col :span="3">
            {{$t('configLazyLoad.intelligentQualityCheck.scoreTimes')}} <span style="color: red;">*</span> ：</el-col>
          <el-col :span="21"><el-input class="mark_input" v-model="ruleForm.scoreConfig.count"></el-input>
            <questionToolTip  style="margin-left: 20px;" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip1')"></questionToolTip>
          </el-col>
        </el-row>
        <el-row class="item-line">
          <el-col :span="3">
            {{$t('intelligentQualityCheck.intelCorrelation')}} <span style="color: red;">*</span> ：</el-col>
          <el-col :span="21">
            <el-select v-model="ruleForm.scoreConfig.parent" @change="scoreConfigChange">
              <el-option v-for="theOne in scoreConfig" :label="theOne.name" :value="theOne._id"></el-option>
            </el-select>
            <div style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
            <el-select v-model="ruleForm.scoreConfig.sub" @change="scoreConfigSubChange">
              <el-option v-for="theTwo in scoreConfigSub" :key="theTwo._id" :label="theTwo.name" :value="theTwo._id"></el-option>
            </el-select>
            <span style="color: #ff6b6b;margin-left: 20px;">{{scoreConfigSubScore}}</span>
            <questionToolTip style="margin-left: 20px;" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip2')"></questionToolTip>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <div style="border-bottom: 1px solid #ccc;"></div>
    <el-row :gutter="60">
      <el-col :span="4" style="font-size:14px;font-weight:bolder;">{{$t('configLazyLoad.intelligentQualityCheck.ruleConfig')}}</el-col>
      <el-col :span="20">
        <el-row class="item-line" style="font-weight: 700;border-bottom: 1px solid #ccc;">
          <el-col :span="2">{{$t('configLazyLoad.intelligentQualityCheck.number')}}</el-col>
          <el-col :span="20">{{$t('configLazyLoad.intelligentQualityCheck.config')}}</el-col>
          <el-col :span="2">{{$t('public.operate')}}</el-col>
        </el-row>
            <!--通话规则配置     form表单==================
            ====================================================-->
        <el-row class="item-line" v-for="(item, index) in ruleListCall" style="border-bottom: 1px solid #ccc;" v-show="ruleForm.type === 'call'">
          <el-col :span="2" style="font-weight: 700;">{{index + 1}}</el-col>
          <el-col :span="20">
            <el-form label-width="80px">
              <el-form-item :label="$t('public.type')">
                <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.type" @change="ruleTypeChange(item)">
                  <el-option v-for="one in ruleTypeList" :label="one.name" :value="one.type"></el-option>
                </el-select>
                <questionToolTip v-show="item.type === 'keywordCheck'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip3')"></questionToolTip>
                <questionToolTip v-show="item.type === 'interrupt'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip4')"></questionToolTip>
                <questionToolTip v-show="item.type === 'firstResponse'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip5')"></questionToolTip>
                <questionToolTip v-show="item.type === 'talkSpeed'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip6')"></questionToolTip>
                <questionToolTip v-show="item.type === 'regCheck'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip7')"></questionToolTip>
                <questionToolTip v-show="item.type === 'talkInterval'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip8')"></questionToolTip>
                <questionToolTip v-show="item.type === 'regRepeat'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip9')"></questionToolTip>
                <questionToolTip v-show="item.type === 'moodTest'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip10')"></questionToolTip>
              </el-form-item>
              <!--关键词匹配原子===========================================-->
              <div v-show="item.type === 'keywordCheck'">
                <el-form-item :label="$t('config.agent.role')">
                  <el-radio-group v-model="item.role">
                    <el-radio label="agent">{{$t('public.agent')}}</el-radio>
                    <el-radio label="visitor">{{$t('public.customer')}}</el-radio>
                    <el-radio label="all">{{$t('public.all')}}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('report.imKeywords')">
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.strategy">
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.anyOne')" value="any"></el-option>
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.all')" value="all"></el-option>
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.none')" value="none"></el-option>
                  </el-select>
                  <el-input v-model="item.wordsCon" class="key-word-input" :maxlength="8" :placeholder="$t('configLazyLoad.intelligentQualityCheck.addKeyWords')"></el-input><el-button type="text" class="key-word-add" @click="addKeyWords(index)">{{$t('public.add')}}</el-button>
                  <el-upload
                     style="display:inline-block"
                     accept=".txt"
                    :limit="1"
                    :auto-upload="true"
                    :data="uploadData"
                    :before-upload="handleBeforeUpload"
                    action=""
                  >
                    <!-- <el-tooltip effect="dark" :content='importVoice' :openDelay="200" transition="fade-leave" placement="top"> -->
                     <el-button type="text" class="key-word-add" @click="importKeyWords(index)">{{$t('public.import')}}</el-button>
                    <!-- </el-tooltip> -->
                  </el-upload>
                <el-button type="text" class="key-word-add" @click="exportKeyWords(index)">{{$t('public.export')}}</el-button>
                </el-form-item>
                <el-form-item>
                  <div class="key-words-wrap">
                    <span v-for="(alone, num) in item.words" class="key-words">
                      {{alone}}
                      <i class="el-icon-error" @click="delKeyWords(index, num)"></i>
                    </span>
                  </div>
                </el-form-item>
                <el-form-item :label="$t('config.agent.matchingType')">
                  <el-radio-group v-model="item.checkType">
                    <el-radio label="allDialogues">{{$t('config.agent.allDialogues')}}</el-radio>
                    <el-radio label="specificWordsContext">{{$t('config.agent.specificWordsContext')}}</el-radio>
                    <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip23')"></questionToolTip>
                  </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('report.specificWords')" v-show="item.checkType=='specificWordsContext'">
                  <el-input v-model="item.specificWordsCon"  :maxlength="8" :placeholder="$t('configLazyLoad.intelligentQualityCheck.addSpecificWords')"></el-input><el-button type="text" class="key-word-add" @click="addSpecificWords(index)">{{$t('public.add')}}</el-button>
                </el-form-item>
                <el-form-item v-show="item.checkType=='specificWordsContext'">
                  <div class="key-words-wrap">
                    <span v-for="(alone, num) in item.specificWords" class="key-words">
                      {{alone}}
                      <i class="el-icon-error" @click="delSpecificWords(index, num)"></i>
                    </span>
                  </div>
                </el-form-item>
              </div>
              <!--服务抢断原子===========================================-->
              <div v-show="item.type === 'interrupt'">
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.interceptionTime')">
                  <el-select :placeholder="$t('public.pleasePick')" style="margin-right: 40px;" v-model="item.interruptTime.type">
                    <el-option v-for="one in symbolArr" :label="one.name" :value="one.type"></el-option>
                  </el-select>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 160px;" type="number" v-model="item.interruptTime.min" v-show="item.interruptTime.type !== 'less'">
                    <template slot="append">{{$t('configLazyLoad.intelligentQualityCheck.millisecond')}}</template>
                  </el-input>
                  <div v-show="item.interruptTime.type === 'range'" style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 160px;" type="number" v-model="item.interruptTime.max" v-show="item.interruptTime.type !== 'greater'">
                    <template slot="append">{{$t('configLazyLoad.intelligentQualityCheck.millisecond')}}</template>
                  </el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip11')"></questionToolTip>
                </el-form-item>
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.interceptionTimes')">
                  <el-select :placeholder="$t('public.pleasePick')" style="margin-right: 40px;" v-model="item.interruptCount.type">
                    <el-option v-for="one in symbolArr" :label="one.name" :value="one.type"></el-option>
                  </el-select>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 160px;" type="number" v-model="item.interruptCount.min" v-show="item.interruptCount.type !== 'less'">
                    <template slot="append">{{$t('configLazyLoad.intelligentQualityCheck.times')}}</template>
                  </el-input>
                  <div v-show="item.interruptCount.type === 'range'" style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 160px;" type="number" v-model="item.interruptCount.max" v-show="item.interruptCount.type !== 'greater'">
                    <template slot="append">{{$t('configLazyLoad.intelligentQualityCheck.times')}}</template>
                  </el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip12')"></questionToolTip>
                </el-form-item>
              </div>
              <!--首次响应原子===========================================-->
              <div v-show="item.type === 'firstResponse'">
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.responseTime')">
                        <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" style="margin-right: 20px;" v-model="item.responseTime.type">
                    <el-option v-for="one in symbolArr" :label="one.name" :value="one.type"></el-option>
                  </el-select>
                       <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.responseTime.min" v-show="item.responseTime.type !== 'less'">
                    <template slot="append">{{$t('public.seconds')}}</template>
                  </el-input>
                      <div v-show="item.responseTime.type === 'range'" style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                    <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.responseTime.max" v-show="item.responseTime.type !== 'greater'">
                      <template slot="append">{{$t('public.seconds')}}</template>
                    </el-input>
                </el-form-item>
              </div>
              <!--语速检测原子===========================================-->
              <div v-show="item.type === 'talkSpeed'">
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.speedOfSpeech')">
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" style="margin-right: 40px;" v-model="item.speedRange.type">
                    <el-option v-for="one in symbolArr" :label="one.name" :value="one.type"></el-option>
                  </el-select>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 160px;" type="number" v-model="item.speedRange.min" v-show="item.speedRange.type !== 'less'">
                    <template slot="append">{{$t('configLazyLoad.intelligentQualityCheck.words/min')}}</template>
                  </el-input>
                  <div v-show="item.speedRange.type === 'range'" style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 160px;" type="number" v-model="item.speedRange.max" v-show="item.speedRange.type !== 'greater'">
                    <template slot="append">{{$t('configLazyLoad.intelligentQualityCheck.words/min')}}</template>
                  </el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip13')"></questionToolTip>
                </el-form-item>
              </div>
              <!--正则匹配原子===========================================-->
              <div v-show="item.type === 'regCheck'">
                <el-form-item :label="$t('config.agent.role')">
                  <el-radio-group v-model="item.role">
                    <el-radio label="agent">{{$t('public.agent')}}</el-radio>
                    <el-radio label="visitor">{{$t('public.customer')}}</el-radio>
                    <el-radio label="all">{{$t('public.all')}}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.regularExpression')">
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 440px;" v-model="item.regStr"></el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip14')"></questionToolTip>
                </el-form-item>
              </div>
              <!--对话时间间隔原子===========================================-->
              <div v-show="item.type === 'talkInterval'">
                <el-form-item :label="$t('config.agent.role')">
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.checkType">
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.roleInsensitive')" value="adjacent"></el-option>
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.differentRroles')" value="distinct"></el-option>
                  </el-select>
                  <div v-show="item.checkType === 'adjacent'">
                    <span style="margin: 0 14px;font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.ruleTip15')}}</span>
                    <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.intervalTime">
                      <template slot="append">{{$t('public.seconds')}}</template>
                    </el-input>
                    <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip16')"></questionToolTip>
                  </div>
                  <div v-show="item.checkType === 'distinct'">
                    <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.distinctType">
                      <el-option :label="$t('configLazyLoad.intelligentQualityCheck.lastStartTime')" value="startTime"></el-option>
                      <el-option :label="$t('configLazyLoad.intelligentQualityCheck.lastEndTime')" value="endTime"></el-option>
                    </el-select>
                    <span style="margin: 0 14px;font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.ruleTip17')}}</span>
                    <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.intervalTime">
                      <template slot="append">{{$t('public.seconds')}}</template>
                    </el-input>
                  </div>
                </el-form-item>
              </div>
              <!--上下文重复原子===========================================-->
              <div v-show="item.type === 'regRepeat'">
                <el-form-item :label="$t('config.agent.role')">
                  <span style="font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.ruleTip18')}}</span>
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" style="margin: 0 14px;" v-model="item.role">
                    <el-option :label="$t('public.agent')" value="agent"></el-option>
                    <el-option :label="$t('public.customer')" value="visitor"></el-option>
                  </el-select>
                  <span style="font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.said')}}</span>
                </el-form-item>
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.regularExpression')">
                  <el-input :placeholder="$t('public.pleaseInput')" v-model="item.regStr" style="width: 440px;"></el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip19')"></questionToolTip>
                </el-form-item>
              </div>
              <!-- 情绪检测原子 -->
              <div v-show="item.type === 'moodTest'">
                 <el-form-item :label="$t('config.agent.role')">
                  <el-radio-group v-model="item.role">
                    <el-radio label="agent">{{$t('public.agent')}}</el-radio>
                    <el-radio label="visitor">{{$t('public.customer')}}</el-radio>
                    <el-radio label="all">{{$t('public.all')}}</el-radio>
                  </el-radio-group>
              </el-form-item>
              </div>
            </el-form>
   
            <el-form label-width="80px"  v-show="item.type !== 'firstResponse'">
            
              <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.matchingRange')">
                <el-input :placeholder="$t('public.pleaseInput')" type="number" v-model="item.checkRange.start"></el-input>
                <div style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                <el-input :placeholder="$t('public.pleaseInput')" type="number" v-model.numer="item.checkRange.end"></el-input>
                <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip24')"></questionToolTip>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="2">
            <span @click="delRuleManageItem(index)" style="color: #ff6b6b;cursor: pointer">{{$t('public.delete')}}</span>
          </el-col>
        </el-row>
            <!--在线咨询规则配置     form表单==================
            ====================================================-->
        <el-row class="item-line" v-for="(item, index) in ruleListWebchat" style="border-bottom: 1px solid #ccc;" v-show="ruleForm.type === 'webchat'">
          <el-col :span="2" style="font-weight: 700;">{{index + 1}}</el-col>
          <el-col :span="20">
            <el-form label-width="80px">
              <el-form-item :label="$t('public.type')">
                <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.type" @change="ruleTypeChange(item)">
                  <el-option v-for="one in ruleTypeList" v-if="one.type !== 'interrupt' && one.type !== 'talkSpeed'" :label="one.name" :value="one.type"></el-option>
                </el-select>
                <questionToolTip v-show="item.type === 'keywordCheck'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip3')"></questionToolTip>
                <questionToolTip v-show="item.type === 'firstResponse'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip5')"></questionToolTip>
                <questionToolTip v-show="item.type === 'regCheck'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip7')"></questionToolTip>
                <questionToolTip v-show="item.type === 'talkInterval'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip8')"></questionToolTip>
                <questionToolTip v-show="item.type === 'regRepeat'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip9')"></questionToolTip>
                <questionToolTip v-show="item.type === 'moodTest'" class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip10')"></questionToolTip>
              </el-form-item>
              <!--关键词匹配原子===========================================-->
              <div v-show="item.type === 'keywordCheck'">
                <el-form-item :label="$t('config.agent.role')">
                  <el-radio-group v-model="item.role">
                    <el-radio label="agent">{{$t('public.agent')}}</el-radio>
                    <el-radio label="visitor">{{$t('public.customer')}}</el-radio>
                    <el-radio label="all">{{$t('public.all')}}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('report.imKeywords')">
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.strategy">
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.anyOne')" value="any"></el-option>
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.all')" value="all"></el-option>
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.none')" value="none"></el-option>
                  </el-select>
                  <el-input v-model="item.wordsCon" class="key-word-input" :maxlength="8" :placeholder="$t('configLazyLoad.intelligentQualityCheck.addKeyWords')"></el-input><el-button type="text" class="key-word-add" @click="addKeyWords(index)">{{$t('public.add')}}</el-button>
                  <el-upload
                     style="display:inline-block"
                     accept=".txt"
                    :limit="1"
                    :auto-upload="true"
                    :data="uploadData"
                    :before-upload="handleBeforeUpload"
                    action=""
                  >
                    <!-- <el-tooltip effect="dark" :content='importVoice' :openDelay="200" transition="fade-leave" placement="top"> -->
                     <el-button type="text" class="key-word-add" @click="importKeyWords(index)">{{$t('public.import')}}</el-button>
                    <!-- </el-tooltip> -->
                    <!-- <el-button type="text" class="key-word-add" @click="importKeyWords(index)">{{$t('public.import')}}</el-button> -->
                  </el-upload>
                <el-button type="text" class="key-word-add" @click="exportKeyWords(index)">{{$t('public.export')}}</el-button>
                </el-form-item>
                <el-form-item>
                  <div class="key-words-wrap">
                    <span v-for="(alone, num) in item.words" class="key-words">
                      {{alone}}
                      <i class="el-icon-error" @click="delKeyWords(index, num)"></i>
                    </span>
                  </div>
                </el-form-item>
                    <el-form-item :label="$t('config.agent.matchingType')">
                  <el-radio-group v-model="item.checkType">
                    <el-radio label="allDialogues">{{$t('config.agent.allDialogues')}}</el-radio>
                    <el-radio label="specificWordsContext">{{$t('config.agent.specificWordsContext')}}</el-radio>
                    <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip23')"></questionToolTip>
                  </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('report.specificWords')" v-show="item.checkType=='specificWordsContext'">
                  <el-input v-model="item.specificWordsCon"  :maxlength="8" :placeholder="$t('configLazyLoad.intelligentQualityCheck.addSpecificWords')"></el-input><el-button type="text" class="key-word-add" @click="addSpecificWords(index)">{{$t('public.add')}}</el-button>
                </el-form-item>
                <el-form-item v-show="item.checkType=='specificWordsContext'">
                  <div class="key-words-wrap">
                    <span v-for="(alone, num) in item.specificWords" class="key-words">
                      {{alone}}
                      <i class="el-icon-error" @click="delSpecificWords(index, num)"></i>
                    </span>
                  </div>
                </el-form-item>
              </div>
              <!--首次响应原子===========================================-->
              <div v-show="item.type === 'firstResponse'">
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.responseTime')">
                          <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" style="margin-right: 20px;" v-model="item.responseTime.type">
                          <el-option v-for="(one,index) in symbolArr" :key='index' :label="one.name" :value="one.type"></el-option>
                        </el-select>
                        <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.responseTime.min" v-show="item.responseTime.type !== 'less'">
                        <template slot="append">{{$t('public.seconds')}}</template>
                      </el-input>
                      <div v-show="item.responseTime.type === 'range'" style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                          <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.responseTime.max" v-show="item.responseTime.type !== 'greater'">
                      <template slot="append">{{$t('public.seconds')}}</template>
                    </el-input>
                </el-form-item>
              </div>
              <!--正则匹配原子===========================================-->
              <div v-show="item.type === 'regCheck'">
                <el-form-item :label="$t('config.agent.role')">
                  <el-radio-group v-model="item.role">
                    <el-radio label="agent">{{$t('public.agent')}}</el-radio>
                    <el-radio label="visitor">{{$t('public.customer')}}</el-radio>
                    <el-radio label="all">{{$t('public.all')}}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.regularExpression')">
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 440px;" v-model="item.regStr"></el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip14')"></questionToolTip>
                </el-form-item>
              </div>
              <!--对话时间间隔原子===========================================-->
              <div v-show="item.type === 'talkInterval'">
                <el-form-item :label="$t('config.agent.role')">
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" v-model="item.checkType">
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.roleInsensitive')" value="adjacent"></el-option>
                    <el-option :label="$t('configLazyLoad.intelligentQualityCheck.differentRroles')" value="distinct"></el-option>
                  </el-select>
                  <!--<div v-show="item.checkType === 'adjacent'">-->
                    <!--<span style="margin: 0 14px;">后一句开始时间，减去前一句结束时间，时间间隔超过</span>-->
                    <!--<el-input placeholder="请输入" style="width: 140px;" type="number" v-model="item.intervalTime">-->
                      <!--<template slot="append">秒</template>-->
                    <!--</el-input>-->
                    <!--<questionToolTip class="ml10" tipContent="座席与客户对话过程中每次对话的间隔时间，只要一句对话的响应时长在指定范围内，就算命中"></questionToolTip>-->
                  <!--</div>-->
                  <!--<div v-show="item.checkType === 'distinct'">-->
                    <!--<el-select placeholder="请选择类型" v-model="item.distinctType">-->
                      <!--<el-option label="后一句开始时间" value="startTime"></el-option>-->
                      <!--<el-option label="后一句结束时间" value="endTime"></el-option>-->
                    <!--</el-select>-->
                  <span style="margin: 0 14px;font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.timeIntervalExceeds')}}</span>
                  <el-input :placeholder="$t('public.pleaseInput')" style="width: 140px;" type="number" v-model="item.intervalTime">
                    <template slot="append">{{$t('public.seconds')}}</template>
                  </el-input>
                  <!--</div>-->
                </el-form-item>
              </div>
              <!--上下文重复原子===========================================-->
              <div v-show="item.type === 'regRepeat'">
                <el-form-item :label="$t('config.agent.role')">
                  <span style="font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.ruleTip18')}}</span>
                  <el-select :placeholder="$t('configLazyLoad.intelligentQualityCheck.chooseType')" style="margin: 0 14px;" v-model="item.role">
                    <el-option :label="$t('public.agent')" value="agent"></el-option>
                    <el-option :label="$t('public.customer')" value="visitor"></el-option>
                  </el-select>
                  <span style="font-size: 12px;">{{$t('configLazyLoad.intelligentQualityCheck.said')}}</span>
                </el-form-item>
                <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.regularExpression')">
                  <el-input :placeholder="$t('public.pleaseInput')" v-model="item.regStr" style="width: 440px;"></el-input>
                  <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip14')"></questionToolTip>
                </el-form-item>
              </div>
            </el-form>
            <el-form label-width="80px"  v-show="item.type !== 'firstResponse'">
              <el-form-item :label="$t('configLazyLoad.intelligentQualityCheck.matchingRange')">
                <el-input :placeholder="$t('public.pleaseInput')" type="number" v-model="item.checkRange.start"></el-input>
                <div style="border-bottom: 2px solid #ccc;margin: -30px 6px 4px;width: 30px;display: inline-block;"></div>
                <el-input :placeholder="$t('public.pleaseInput')" type="number" v-model="item.checkRange.end"></el-input>
                <questionToolTip class="ml10" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip24')"></questionToolTip>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="2">
            <span @click="delRuleManageItem(index)" style="color: #ff6b6b;cursor: pointer">{{$t('public.delete')}}</span>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-button type="primary" class="primary_btn fr ml10" @click="addRuleManageItem">{{$t('configLazyLoad.intelligentQualityCheck.addAtom')}}</el-button>
    <div style="clear: both;"></div>
    <div style="border-bottom: 1px solid #ccc;margin: 20px 0;"></div>
    <el-row :gutter="60">
      <el-col :span="4" style="font-size:14px;font-weight:bolder;">
        {{$t('configLazyLoad.intelligentQualityCheck.atomsRelationship')}}
        <questionToolTip :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip21')"></questionToolTip>
      </el-col>
      <el-col :span="20">
        <el-row class="ruleLogic-item-line" v-for="(item, index) in ruleForm.ruleLogic">
          <el-checkbox-group style="display: inline-block;margin-right: 30px;" v-model="item.keys">
            <el-checkbox v-for="(item, index) in ruleForm.type === 'call' ? ruleListCall : ruleListWebchat" :label="index + 1"></el-checkbox>
          </el-checkbox-group>
          <el-select :placeholder="$t('public.pleaseInput')" size="mini" style="display: inline-block;margin-right: 20px;" v-model="item.strategy">
            <el-option :label="$t('configLazyLoad.intelligentQualityCheck.anyOne1')" value="any"></el-option>
            <el-option :label="$t('configLazyLoad.intelligentQualityCheck.all1')" value="all"></el-option>
            <el-option :label="$t('configLazyLoad.intelligentQualityCheck.notAll')" value="none"></el-option>
          </el-select>
          <i class="el-icon-plus" v-if="index === ruleForm.ruleLogic.length - 1" @click="addRuleLogicItem"></i>
          <i class="el-icon-minus" v-if="index !== ruleForm.ruleLogic.length - 1" @click="delRuleLogicItem(index)"></i>
          <questionToolTip  v-if="index === 0" :tipContent="$t('configLazyLoad.intelligentQualityCheck.ruleTip22')" style="margin-left: 20px;"></questionToolTip>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import bus from '@/store/eventBus'
  import { deepClone } from '@utils/m7Utils'
  import questionToolTip from '../../../../ui-modules/questionToolTip/QuestionToolTip.vue'
  export default {
    name: 'RuleManageDetail',
    data () {
      return {
        // importVoice:'请导入以回车分割的文本文件',
        uploadData:{
          index:null
        },
        sendding: false,
        state: 'add',
        ruleTitle: this.$t('configLazyLoad.intelligentQualityCheck.addRule'),
        _id: '',
        scoreConfig: [],    //    关联计分项
        scoreConfigSub: [],  //    关联计分项子项
        scoreConfigSubScore: 0,   //   关联计分项  分数
        ruleItem: {
          type: 'keywordCheck',
          strategy: 'any',
          role: 'agent',
          words: [],
          wordsCon:'',
          specificWordsCon:'',
          specificWords: [],
          checkType: 'allDialogues',
          regStr: '',
          distinctType: 'startTime',
          intervalTime: 0,
          interruptTime: {
            min: 0,
            max: 100,
            type: 'range'
          },
          interruptCount: {
            min: 0,
            max: 100,
            type: 'range'
          },
          responseTime: {
            min: 0,
            max: 100,
            type: 'range'
          },
          speedRange: {
            min: 0,
            max: 100,
            type: 'range'
          },
          checkRange: {
            start: 1,
            end: -1
          }
        },
        ruleTypeList: [
          {name: this.$t('configLazyLoad.intelligentQualityCheck.keyWordsMatch'), type: 'keywordCheck'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.serviceHijacking'), type: 'interrupt'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.firstResponse'), type: 'firstResponse'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.speedDetection'), type: 'talkSpeed'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.regularMatch'), type: 'regCheck'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.conversationInterval'), type: 'talkInterval'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.contextRepetition'), type: 'regRepeat'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.emotionDetection'), type: 'moodTest'}
        ],
        symbolArr: [
          {name: this.$t('public.lessThan'), type: 'less'},
          {name: this.$t('public.moreThan'), type: 'greater'},
          {name: this.$t('configLazyLoad.intelligentQualityCheck.section'), type: 'range'}
        ],
        ruleForm: {
          name: '',
          type: 'call',
          note: '',
          isEnable: true,
          scoreConfig: {
            count: 1,
            parent: '',
            sub: ''
          },
          ruleLogic: [     //    原子间关系逻辑
            {strategy: 'any', keys: []}
          ]
        },
        ruleListCall: [],
        ruleListWebchat: [],
        checkType:false
      }
    },
    methods: {
      goBack () {
        this.$router.go(-1)
      },
//        计分关联项 =   联动 ======
      scoreConfigChange () {
        this.scoreConfig.forEach(item => {
          if (item._id === this.ruleForm.scoreConfig.parent) {
            this.scoreConfigSub = item.subs
            if (item.subs.length !== 0) {
              this.ruleForm.scoreConfig.sub = item.subs[0]._id
            } else {
              this.ruleForm.scoreConfig.sub = ''
            }
            this.scoreConfigSubChange()
            return
          }
        })
      },
      //        计分关联项   子项 = ======
      scoreConfigSubChange () {
        // console.log(this.scoreConfigSub)
        // console.log(this.ruleForm.scoreConfig.sub)
        if (this.ruleForm.scoreConfig.sub === '') {
          this.scoreConfigSubScore = 0
          return
        }
        this.scoreConfigSub.forEach(item => {
          if (item._id === this.ruleForm.scoreConfig.sub) {
            this.scoreConfigSubScore = item.score
            return
          }
        })
      },
//      添加规则管理  原子
      addRuleManageItem () {
        let item = deepClone(this.ruleItem)
        if (this.ruleForm.type === 'call') {
          this.ruleListCall.push(item)
        } else {
          this.ruleListWebchat.push(item)
        }
      },
      //      删除规则管理  原子
      delRuleManageItem (index) {
        if (this.ruleForm.type === 'call') {
          this.ruleListCall.splice(index, 1)
        } else {
          this.ruleListWebchat.splice(index, 1)
        }
      },
//      添加原子逻辑
      addRuleLogicItem () {
        this.ruleForm.ruleLogic.push({strategy: 'any', keys: []})
      },
      delRuleLogicItem (index) {
        this.ruleForm.ruleLogic.splice(index, 1)
      },
//      添加删除关键字
      delKeyWords (index, num) {
        if (this.ruleForm.type === 'call') {
          this.ruleListCall[index].words.splice(num, 1)
        } else {
          this.ruleListWebchat[index].words.splice(num, 1)
        }
      },
//      添加删除特定上下文
      delSpecificWords (index, num) {
        if (this.ruleForm.type === 'call') {
          this.ruleListCall[index].specificWords.splice(num, 1)
        } else {
          this.ruleListWebchat[index].specificWords.splice(num, 1)
        }
      },
      addKeyWords (index) {
        if (this.ruleForm.type === 'call') {
          if (!this.ruleListCall[index].wordsCon) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError13'))
            return
          }
          if (this.ruleListCall[index].words.indexOf(this.ruleListCall[index].wordsCon) !== -1) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError14'))
            return
          }
          this.ruleListCall[index].words.push(this.ruleListCall[index].wordsCon)
          this.ruleListCall[index].wordsCon = ''
        } else {
          if (!this.ruleListWebchat[index].wordsCon) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError13'))
            return
          }
          if (this.ruleListWebchat[index].words.indexOf(this.ruleListWebchat[index].wordsCon) !== -1) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError14'))
            return
          }
          this.ruleListWebchat[index].words.push(this.ruleListWebchat[index].wordsCon)
          this.ruleListWebchat[index].wordsCon = ''
        }
      },
      //添加特定词语
      addSpecificWords (index) {
        if (this.ruleForm.type === 'call') {
          if (!this.ruleListCall[index].specificWordsCon) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError18'))
            return
          }
          if (this.ruleListCall[index].specificWords.indexOf(this.ruleListCall[index].specificWordsCon) !== -1) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError19'))
            return
          }
          this.ruleListCall[index].specificWords.push(this.ruleListCall[index].specificWordsCon)
          this.ruleListCall[index].specificWordsCon = ''
        } else {
          if (!this.ruleListWebchat[index].specificWordsCon) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError18'))
            return
          }
          if (this.ruleListWebchat[index].specificWords.indexOf(this.ruleListWebchat[index].specificWordsCon) !== -1) {
            this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError19'))
            return
          }
          this.ruleListWebchat[index].specificWords.push(this.ruleListWebchat[index].specificWordsCon)
          this.ruleListWebchat[index].specificWordsCon = ''
        }
      },
      handleBeforeUpload(file){
        this.fileList = [file]
        if (file.size / 1024 / 1024 > 1) {
          this.$message.error("上传文件大小不能超过 1M !");
          return false;
        }
        console.log('选择了文件beforeUpload')
        // 读取数据
        this.read(file);
        return false
      },
      read(f) {
          let reader = new FileReader();
          reader.onload = e => { 
          let data = e.target.result;
          data = new Uint8Array(data);
          var encoding = this.isUTF8(data) ? "UTF8" : "GBK"; //这里只做了判断是否是UTF8判断，如果不是，就假定是GBK,没做其它编码的处理。
          if (encoding == "GBK") {
            var rd = new FileReader();
            rd.onload = (e) => {
              let result = e.target.result;
              if(result){
                var contList = result.split("\r\n");
                for(var i =0;i<contList.length;i++){
                    if(contList[i] == ""){
                      contList.splice(i, 1);
                      //删除数组索引位置应保持不变
                      i--;
                    }
                }
              }
              if (this.ruleForm.type === 'call') {
                  contList.forEach(item=>{
                    if (this.ruleListCall[this.uploadData.index].words.indexOf(item) == -1) {
                      this.ruleListCall[this.uploadData.index].words.push(item)
                    }else{
                      this.$message.success(this.$t('configLazyLoad.intelligentQualityCheck.importInfo'))
                    }
                    this.ruleListCall[this.uploadData.index].wordsCon = ''
                  })
              }else {
                  contList.forEach(item=>{
                    if (this.ruleListWebchat[this.uploadData.index].words.indexOf(item) == -1) {
                      this.ruleListWebchat[this.uploadData.index].words.push(item)
                    }else{
                      this.$message.success(this.$t('configLazyLoad.intelligentQualityCheck.importInfo'))
                    }
                    this.ruleListWebchat[this.uploadData.index].wordsCon = ''
                  })
              };
            };
            rd.readAsText(f, "gb2312");
          }else{
            var r = new FileReader();
            r.onload = (e) => {
              let result = e.target.result;
              if(result){
                var contList = result.split("\r\n");
                for(var i =0;i<contList.length;i++){
                    if(contList[i] == ""){
                      contList.splice(i, 1);
                      //删除数组索引位置应保持不变
                      i--;
                    }
                }
              }
              if (this.ruleForm.type === 'call') {
                  contList.forEach(item=>{
                    if (this.ruleListCall[this.uploadData.index].words.indexOf(item) == -1) {
                      this.ruleListCall[this.uploadData.index].words.push(item)
                    }else{
                      this.$message.success(this.$t('configLazyLoad.intelligentQualityCheck.importInfo'))
                    }
                    this.ruleListCall[this.uploadData.index].wordsCon = ''
                  })
              }else {
                  contList.forEach(item=>{
                    if (this.ruleListWebchat[this.uploadData.index].words.indexOf(item) == -1) {
                      this.ruleListWebchat[this.uploadData.index].words.push(item)
                    }else{
                      this.$message.success(this.$t('configLazyLoad.intelligentQualityCheck.importInfo'))
                    }
                    this.ruleListWebchat[this.uploadData.index].wordsCon = ''
                  })
              };
            };
            r.readAsText(f, "UTF-8");
          } 
       }
        reader.readAsArrayBuffer(f);
      },
      //导入
      importKeyWords (index) {
        this.uploadData.index = index;
      },
      //判断编码格式是否是utf-8
      isUTF8(bytes) {
        var i = 0;
        while (i < bytes.length) {
          if (
            // ASCII
            bytes[i] == 0x09 ||
            bytes[i] == 0x0a ||
            bytes[i] == 0x0d ||
            (0x20 <= bytes[i] && bytes[i] <= 0x7e)
          ) {
            i += 1;
            continue;
          }

          if (
            // non-overlong 2-byte
            0xc2 <= bytes[i] &&
            bytes[i] <= 0xdf &&
            0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xbf
          ) {
            i += 2;
            continue;
          }

          if (
            // excluding overlongs
            (bytes[i] == 0xe0 &&
              0xa0 <= bytes[i + 1] && bytes[i + 1] <= 0xbf &&
              0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xbf) || // straight 3-byte
            (((0xe1 <= bytes[i] && bytes[i] <= 0xec) ||
              bytes[i] == 0xee ||
              bytes[i] == 0xef) &&
              0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xbf &&
              0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xbf) || // excluding surrogates
            (bytes[i] == 0xed &&
              0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x9f &&
              0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xbf)
          ) {
            i += 3;
            continue;
          }

          if (
            // planes 1-3
            (bytes[i] == 0xf0 &&
              0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xbf &&
              0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xbf &&
              0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xbf) || // planes 4-15
            (0xf1 <= bytes[i] &&
              bytes[i] <= 0xf3 &&
              0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xbf &&
              0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xbf &&
              0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xbf) || // plane 16
            (bytes[i] == 0xf4 &&
              0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8f &&
              0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xbf &&
              0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xbf)
          ) {
            i += 4;
            continue;
          }
          return false;
        }
        return true;
      },
      //导出
      exportKeyWords (index) {
        var str=''
        if (this.ruleForm.type === 'call') {
            this.ruleListCall[index].words.forEach(item=>{
                str+=item+'\r\n'
            })  
        }else {
            this.ruleListWebchat[index].words.forEach(item=>{
                str+=item+'\r\n'
            })  
        };
        var allStr = str
        var export_blob = new Blob([allStr],{type: "data:text/plain;charset=ANSI"})
        var save_link = document.createElement("a")
        save_link.href = window.URL.createObjectURL(export_blob)
        save_link.download = '关键字导出'+new Date().valueOf()+'.txt'
        this.fakeClick(save_link)
      },
      
      fakeClick(obj) {
        var ev = document.createEvent("MouseEvents");
        ev.initMouseEvent("click",true,false,window,0,0,0,0,0,false,false,false,false,0,null);
        obj.dispatchEvent(ev);
      },

      base64 (s) { return window.btoa(unescape(encodeURIComponent(s))) },
//      校验提交的form数据
      checkRuleform () {
        let ruleList = []
        if (this.ruleForm.type === 'call') {
          ruleList = this.ruleListCall
        } else {
          ruleList = this.ruleListWebchat
        }
        this.ruleForm.ruleList = this.resolveForm(ruleList)
        if (!this.ruleForm.name) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError3'))
          return false
        }
        if (!this.ruleForm.type) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError4'))
          return false
        }
        if (!this.ruleForm.scoreConfig.count) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError5'))
          return false
        }
        if (!this.ruleForm.scoreConfig.parent || !this.ruleForm.scoreConfig.sub) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError6'))
          return false
        }
        if (this.ruleForm.ruleList.length === 0) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError7'))
          return false
        }
        let checkFlag = true
        this.ruleForm.ruleList.forEach(item => {
          if (item.checkRange) {
            item.checkRange.start = Number(item.checkRange.start)
            item.checkRange.end = Number(item.checkRange.end)
          }
          if (item.type === 'keywordCheck') {
            if (item.words.length === 0) {
              this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError8'))
              checkFlag = false
              return false
            }
            if (item.checkType=='specificWordsContext'&&item.specificWords.length === 0) {
              this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError17'))
              checkFlag = false
              return false
            }
          } else if (item.type === 'interrupt') {
            item.interruptCount.min = Number(item.interruptCount.min)
            item.interruptCount.max = Number(item.interruptCount.max)
            item.interruptTime.min = Number(item.interruptTime.min)
            item.interruptTime.max = Number(item.interruptTime.max)
            if (!this.checkItem(item.interruptCount.type, item.interruptCount.min, item.interruptCount.max, this.$t('configLazyLoad.intelligentQualityCheck.ruleError9'),this.$t('configLazyLoad.intelligentQualityCheck.ruleError20'))) {
              checkFlag = false
              return false
            }
            if (!this.checkItem(item.interruptTime.type, item.interruptTime.min, item.interruptTime.max, this.$t('configLazyLoad.intelligentQualityCheck.ruleError10'),this.$t('configLazyLoad.intelligentQualityCheck.ruleError21'))) {
              checkFlag = false
              return false
            }
          } else if (item.type === 'firstResponse') {
            item.responseTime.min = Number(item.responseTime.min)
            item.responseTime.max = Number(item.responseTime.max)
            if(item.responseTime.type=="greater"){
              item.responseTime.max = 1296000 //最大限15天毫秒数
            }else if(item.responseTime.type=="less"){
              item.responseTime.min = 0 //最下限
            }
            if (!this.checkItem(item.responseTime.type, item.responseTime.min, item.responseTime.max, this.$t('configLazyLoad.intelligentQualityCheck.ruleError11'),this.$t('configLazyLoad.intelligentQualityCheck.ruleError22'))) {
              checkFlag = false
              return false
            }
          } else if (item.type === 'talkSpeed') {
            item.speedRange.min = Number(item.speedRange.min)
            item.speedRange.max = Number(item.speedRange.max)
            if (!this.checkItem(item.speedRange.type, item.speedRange.min, item.speedRange.max, this.$t('configLazyLoad.intelligentQualityCheck.ruleError12'),this.$t('configLazyLoad.intelligentQualityCheck.ruleError23'))) {
              checkFlag = false
              return false
            }
          } else if (item.type === 'regCheck') {
          } else if (item.type === 'talkInterval') {
            item.intervalTime = Number(item.intervalTime)
            if (item.intervalTime < 0) {
              this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError15'))
              checkFlag = false
              return false
            }
          } else if (item.type === 'regRepeat') {
          } else if (item.type === 'moodTest') {
          }
        })
//        处理原子间逻辑  当删除原子时  原子间逻辑keys也要及时删除
        let ruleListLength = this.ruleForm.ruleList.length
        let ruleLogic = deepClone(this.ruleForm.ruleLogic)
        let keysFlag = false    //  保证至少有一组 原子间逻辑
        ruleLogic.forEach((item, index) => {
          this.ruleForm.ruleLogic[index].keys = []
          item.keys.forEach((one) => {
            if (one <= ruleListLength) {
              this.ruleForm.ruleLogic[index].keys.push(one)
            }
          })
          if (item.keys.length > 0) {
            keysFlag = true
          }
        })
        if (!keysFlag) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError16'))
          checkFlag = false
          return false
        }
        if (checkFlag === false) {
          return false
        } else {
          return true
        }
      },
//      解析规则原子form并校验
      resolveForm (dataArr) {
        let data = []
        dataArr.forEach(item => {
          let one = {}
          one.type = item.type
          if (item.type === 'keywordCheck') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.keyWordsMatch')
            one.strategy = item.strategy
            one.role = item.role
            one.checkType = item.checkType
            one.words = item.words
            if(one.checkType=='specificWordsContext'){
              one.specificWords = item.specificWords
            }else{
              one.specificWords = []
            }
            one.checkRange = item.checkRange
          } else if (item.type === 'interrupt') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.serviceHijacking')
            one.interruptCount = item.interruptCount
            one.interruptTime = item.interruptTime
            one.checkRange = item.checkRange
          } else if (item.type === 'firstResponse') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.firstResponse')
            one.responseTime = item.responseTime
          } else if (item.type === 'talkSpeed') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.speedDetection')
            one.speedRange = item.speedRange
            one.checkRange = item.checkRange
          } else if (item.type === 'regCheck') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.regularMatch')
            one.role = item.role
            one.regStr = item.regStr
            one.checkRange = item.checkRange
          } else if (item.type === 'talkInterval') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.conversationInterval')
            one.checkType = item.checkType
            one.distinctType = item.distinctType
            one.intervalTime = item.intervalTime
            one.checkRange = item.checkRange
          } else if (item.type === 'regRepeat') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.contextRepetition')
            one.role = item.role
            one.regStr = item.regStr
            one.checkRange = item.checkRange
          } else if (item.type === 'moodTest') {
            one.name = this.$t('configLazyLoad.intelligentQualityCheck.emotionDetection')
            one.role = item.role
            one.checkRange = item.checkRange
          }
          data.push(one)
        })
        return data
      },
      checkItem (val1, val2, val3, val4, val5) {
        if (val1 === 'range') {
          if (val2 < 0 || val3 < val2) {
            this.$message.error(val4)
            return false
          }
        }else if (val3 < 0 || val2 < 0) {
          this.$message.error(val5)
          return false
        }
        return true
      },
//      保存 ==============
      saveAndUse () {
        this.ruleForm.isEnable = true
        if (!this.checkRuleform()) {
          return
        }
        this.save()
      },
      justSave () {
        this.ruleForm.isEnable = false
        if (!this.checkRuleform()) {
          return
        }
        this.save()
      },
      save () {
        if (this.ruleForm.scoreConfig.parent === this.$t('configLazyLoad.intelligentQualityCheck.itemDeleted') || this.ruleForm.scoreConfig.sub === this.$t('configLazyLoad.intelligentQualityCheck.itemDeleted')) {
          this.$message.error(this.$t('configLazyLoad.intelligentQualityCheck.ruleError6'))
          return false
        }
        this.sendding = true
        if (this._id) {
          this.ruleForm._id = this._id
          this.$store.dispatch('updateMarkRuleConfig', this.ruleForm).then(res => {
            if (res.success) {
              this.$message.success(this.$t('business.updateSuccess'))
              this.sendding = false
              this.$router.push({path: '/index/config/intelligentQualityCheckConfig/ruleManage'})
            }
          })
        } else {
          this.$store.dispatch('addMarkRuleConfig', this.ruleForm).then(res => {
            if (res.success) {
              this.$message.success(this.$t('config.customer.addSucc'))
              this.sendding = false
              this.$router.push({path: '/index/config/intelligentQualityCheckConfig/ruleManage'})
            }
          })
        }
      },
      initData (data) {
        this.ruleListCall = []
        this.ruleListWebchat = []
        if (!data) {
          this.ruleTitle = this.$t('configLazyLoad.intelligentQualityCheck.addRule')
          this.state = 'add'
          this._id = ''
          this.ruleForm.name = ''
          this.ruleForm.type = 'call'
          this.ruleForm.note = ''
          this.ruleForm.isEnable = true
          this.ruleForm.scoreConfig = {count: 1, parent: '', sub: ''}
          this.ruleForm.ruleLogic = [{strategy: 'any', keys: []}]
          this.scoreConfigSubScore = 0
          this.ruleForm.scoreConfig.sub = ''
        } else {
          this.ruleTitle = this.$t('configLazyLoad.intelligentQualityCheck.editRule')
          this.state = 'edit'
          this._id = data._id
          this.ruleForm.name = data.name
          this.ruleForm.type = data.type || 'call'
          this.ruleForm.note = data.note
          this.ruleForm.scoreConfig.count = data.scoreConfig.count
          this.ruleForm.scoreConfig.parent = data.scoreConfig.parent._id
          let scoreConfig = []
          this.scoreConfig.forEach(item => {
            scoreConfig.push(item._id)
          })
          if (scoreConfig.indexOf(data.scoreConfig.parent._id) === -1) {
            this.ruleForm.scoreConfig.parent = this.$t('configLazyLoad.intelligentQualityCheck.itemDeleted')
          }
          this.scoreConfigChange()
          this.ruleForm.scoreConfig.sub = data.scoreConfig.sub._id
          this.scoreConfigSubScore = data.scoreConfig.sub.score
          let scoreConfigSub = []
          this.scoreConfigSub.forEach(item => {
            scoreConfigSub.push(item._id)
          })
          if (scoreConfigSub.indexOf(data.scoreConfig.sub._id) === -1) {
            this.ruleForm.scoreConfig.sub = this.$t('configLazyLoad.intelligentQualityCheck.itemDeleted')
          }
          this.ruleForm.ruleLogic = data.ruleLogic
          data.ruleList.forEach((item) => {
            let ruleItem = deepClone(this.ruleItem)
            if(item.type=='keywordCheck'){
              if(!item.checkType){
                item.checkType = 'allDialogues'
              }
            }
            for (let one in item) {
              ruleItem[one] = item[one]
            }
            if (this.ruleForm.type === 'call') {
              this.ruleListCall.push(ruleItem)
            } else {
              this.ruleListWebchat.push(ruleItem)
            }
          })
        }
        // console.log(this.ruleListCall,'this.ruleListCall')
        // console.log(this.ruleListWebchat,'this.ruleListWebchat')
      },
      fetchData () {
        this.$store.dispatch('getScoreConfig', {}).then(res => {
          if (res.success) {
            if (res.data) {
              this.scoreConfig = res.data
              let ruleDetail = deepClone(this.$store.state.config.intelligentQualityCheckConfig.markRuleDetail || null)
              this.initData(ruleDetail)
            }
          }
        })
      },
      ruleTypeChange(item){
        if(item.type=='keywordCheck'){
          item.checkType = 'allDialogues'
        }else{
          item.checkType = 'adjacent'
        }
      }
    },
    components: {
      questionToolTip
    },
    beforeMount () {
      this.fetchData()
    },
    mounted () {
      bus.$on('markRuleInfo', () => {
        this.fetchData()
      })
    },
    beforeDestroy () {
      bus.$off('markRuleInfo')
    },
    activated () {
      this.fetchData()
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../assets/common.styl"
  .rule-manage-detail
    padding 20px
    height calc(100vh - 125px)
    width calc(100%)
    overflow-y auto
    overflow-x hidden
    box-sizing border-box
    .el-row
      padding 10px 0
    .ruleLogic-item-line
      padding-top 0px
    .btn-wrap
      border-bottom 1px solid #ccc
      padding-bottom 20px
      .back-btn
        border-radius 50%
        border 1px solid $c-main
        color $c-main
        padding 2px
        margin-right 6px
      .el-button
        margin-top -4px
        margin-left 20px
    .el-select,.el-input
      width 200px
    i
      cursor pointer
    .el-icon-plus
      font-size 14px
      color $c-main
    .el-icon-minus
      font-size 14px
      color #ff6b6b
    .key-words-wrap
      white-space normal
      padding 10px
      border 1px solid #ccc
      border-radius 6px
      width 420px
      min-height 100px
      margin 10px 0
    .key-word-input
      width 140px
      margin-left 20px
    .key-word-add
      width 40px
      margin-left 10px
      color $c-main
      cursor pointer
    .key-words
      display inline-block
      line-height 30px
      height 30px
      padding 0 12px
      border 1px solid #ccc
      border-radius 4px
      margin-right 16px
      position relative
      .el-icon-error
        position absolute
        color red
        cursor pointer
        right -5px
        top -5px
    .ml10
      margin-left 10px
    .el-input-group--append
      margin-top 5px
</style>
